"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A10检测项,确保backslash_quote参数配置检测与修复成功
Create At   : 2024/11/1
Owner       : lihongji
Description :
    1.设置backslash_quote参数为safe_encoding后检测A10项
    2.对符合安全规范的A10修复后检测,查看backslash_quote的值
    3.设置backslash_quote参数为off后检测A10项
    4.对符合安全规范的A10修复后检测,查看backslash_quote的值
    5.设置backslash_quote参数为on后检测A10项
    6.对不符合安全规范的A10修复后检测,查看backslash_quote的值
Expect      :
    1.设置成功，检测不打印告警信息
    2.修复成功，不打印告警信息，backslash_quote的值为safe_encoding
    3.设置成功，检测不打印告警信息
    4.修复成功，不打印告警信息，backslash_quote的值为off
    5.设置成功，检测打印告警信息
    6.修复成功，不打印告警信息，backslash_quote的值为safe_encoding
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        step = ('----step1:设置backslash_quote参数为safe_encoding后检测A10项'
                'expect:设置成功，检测不打印告警信息----')
        self.log.info(step)
        execute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, 
            "backslash_quote=safe_encoding")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A10 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(execute_msg)
        self.assertNotIn("Ensure correct configuration of the backslash_quote"
                         " parameter.", check_res, '执行失败' + step)

        step = ('----step2:对符合安全规范的A10修复后检测,查看backslash_quote的值'
                'expect:修复成功，不打印告警信息，'
                'backslash_quote的值为safe_encoding----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B10 --detail;'
                          f'gs_checkse -i A10 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show backslash_quote;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('safe_encoding', create_res, '执行失败' + step)
        self.assertNotIn("Ensure correct configuration of the backslash_quote"
                         " parameter.", check_res, '执行失败' + step)
        self.assertIn('[ Set Other configurations ]                    '
                      '        : Normal', check_res, '执行失败' + step)

        step = ('----step3:设置backslash_quote参数为off后检测A10项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        execute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, "backslash_quote=off")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A10 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(execute_msg)
        self.assertNotIn("Ensure correct configuration of the backslash_quote"
                         " parameter.", check_res, '执行失败' + step)

        step = ('----step4:对符合安全规范的A10修复后检测,查看backslash_quote的值'
                'expect:修复成功，不打印告警信息，backslash_quote的值为off----')
        self.log.info(step)
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B10 --detail;'
                          f'gs_checkse -i A10 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show backslash_quote;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('off', create_res, '执行失败' + step)
        self.assertNotIn("Ensure correct configuration of the backslash_quote"
                         " parameter.", check_res, '执行失败' + step)
        self.assertIn('[ Set Other configurations ]                    '
                      '        : Normal', check_res, '执行失败' + step)

        step = ('----step5:设置backslash_quote参数为on后检测A10项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        execute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, "backslash_quote=on")
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A10 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(execute_msg)
        self.assertIn("Ensure correct configuration of the backslash_quote"
                      " parameter.", check_res, '执行失败' + step)

        step = ('----step6:对不符合安全规范的A10修复后检测,查看backslash_quote的值'
                'expect:修复成功，不打印告警信息，backslash_quote的值为off----')
        self.log.info(step)
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B10 --detail;'
                          f'gs_checkse -i A10 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show backslash_quote;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('safe_encoding', create_res, '执行失败' + step)
        self.assertNotIn("Ensure correct configuration of the backslash_quote"
                         " parameter.", check_res, '执行失败' + step)
        self.assertIn('[ Set Other configurations ]                    '
                      '        : Normal', check_res, '执行失败' + step)

    def tearDown(self):
        step = '----step7:清理环境 expect:成功----'
        self.log.info(step)
        recover = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            "backslash_quote=safe_encoding")
        self.assertTrue(recover)
        self.log.info(f'----{os.path.basename(__file__)} end----')